package com.yjd.comm.util;

import com.alibaba.druid.support.json.JSONUtils;
import com.yjd.comm.bank.model.AuthBankModel;
import com.yjd.comm.bank.model.BankCardDataModel;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

public class AuthBankUtils {

    private static Logger logger = Logger.getLogger(AuthBankUtils.class);
    public static String FOUR = "Four"; // 四要素


    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static BankCardDataModel authBank(AuthBankModel model) throws Exception {
		// 调用接口验证通过 更新银行卡信息
		String commUrl = LocalPropertyConfigurer.getContextProperty("inner.auth.bank.url").toString();
		String authKey = LocalPropertyConfigurer.getContextProperty("bank.card.auth.key").toString();

		if (StringUtils.isBlank(commUrl) || StringUtils.isBlank(authKey)) {
			FrameUtil.throwBaseException1("认证接口参数错误!");
		}

		String keyStr = SecurityCode.md5(authKey);
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("user_id", model.getUser_id());
		param.put("bank_card_no", model.getBank_card_no());
		param.put("bank_card_idname", model.getBank_card_idname());
		param.put("bank_card_idcard", model.getBank_card_idcard());
		param.put("bank_card_mobile", model.getBank_card_mobile());
		param.put("request_client", model.getReqeust_client());

		Map request = new HashMap<String, String>();
		request.put("app_user", "100001");
		request.put("app_type", model.getApp_type());
		request.put("app_msg", CryptAES.AES_Encrypt(keyStr, JSONUtils.toJSONString(param)));
		String result = HttpsUtil.requestPostForm(commUrl, request);
		if (!StringUtils.isBlank(result)) {
			Object rsObj = null;
			try {
				rsObj = JSONUtils.parse(result);
			} catch (Exception e) {
				logger.error("请求url:" + commUrl + "绑定银行卡异常 输入参数:" + param + " 返回结果：" + result);
				FrameUtil.throwBaseException1("绑定银行卡异常 ");
			}
			Map<String, String> resMap = (Map<String, String>) rsObj;
			String state = resMap.get("state");
			if (!"1".equals(state)) {
				logger.error("请求url:" + commUrl + "请求参数:" + param + " 返回结果：" + result);
				FrameUtil.throwBaseException1(resMap.get("msg"));
			} else {
				String jsonRes = CryptAES.AES_Decrypt(keyStr,
						resMap.get("data"));
				Map<String, Object> reMap = (Map<String, Object>) JSONUtils
						.parse(jsonRes);
				Map<String, String> appResult = (Map<String, String>) reMap
						.get("AppResult");
				if (!"100".equals(appResult.get("Code"))) {
					logger.error("请求url:" + commUrl + "实名银行卡绑定接口,返回信息：" + jsonRes);
					FrameUtil.throwBaseException1("您的身份信息不匹配,请重新核实.");
				}

				Map<String, String> bankCardData = (Map<String, String>) reMap
						.get("BankCardData");
				logger.error("请求url:" + commUrl + "返回数据：" + bankCardData);
				if (bankCardData != null) {
					BankCardDataModel bankCardModel = new BankCardDataModel();
					bankCardModel.setBank_card_no(bankCardData
							.get("bank_card_no"));
					bankCardModel.setBank_card_type(bankCardData
							.get("bank_card_type"));
					bankCardModel.setBankCode(bankCardData.get("bank_code"));
					if (!"1".equals(bankCardData.get("bank_card_type"))) {
						FrameUtil.throwBaseException1("银行卡必须为借记卡!");
					}
					return bankCardModel;
				}
			}
		} else {
			logger.error("请求url:" + commUrl + "实名银行卡绑定接口,返回信息："
					+ result);
			FrameUtil.throwBaseException1("认证接口调用失败!");
		}
		return null;
	}

}
